/**
 * Created with JetBrains PhpStorm.
 * User: anynix
 * Date: 12-8-5
 * Time: 下午1:34
 * To change this template use File | Settings | File Templates.
 */
$(document).ready(function(){
    getall();

});

var getall = function(){
    $.getJSON('managecontroller/getall', '', function(data){
        var content = '';
        for(i=0; i<data.length; i++){
            var image_link = data[i].IMAGE_LINK;
            var douban_link = data[i].DOUBAN_LINK;
            var book_id = data[i].BOOK_ID;
            content +=
                '<div class="item-subject">'+
                    '<a class="item" href="'+douban_link+'" target="_blank" >'+
                    '<div class="pic">'+
                    '<img height="120" src="'+image_link+'">'+
                    '</div>'+
                    '<div class="title">'+
                    '<h5>'+data[i].BOOK_NAME+'</h5>'+
                    '</div>'+
                    '</a>'+
                    '<div class="desc">'+
                    '<em class="edit" >'+
                    '<a class="modify" id="m_'+book_id+'">编辑</a>'+
                    '<a class="delete" id="d_'+book_id+'">下架</a>'+
                    '</em>'+
                    '</div>'+
                    '</div>';
        }
        $('.section').html(content);
        $('.delete').click(function(){
            var m_id = $(this).attr('id');
            var str = m_id.split(/_/);
            delbook(str[1]);
        });
        $('.modify').click(function(){
            var m_id = $(this).attr('id');
            var str = m_id.split(/_/);
            modify(str[1]);
        });
    });
};

var delbook = function(book_id){
    var term = {
        'book_id' : book_id
    };
    $.getJSON('managecontroller/del', term, function(data){
        if(data){
            getall();
            alert('成功删除');
        }
    });
};

var modify = function(book_id){
    // not finished yet

    var term = {
        'book_id' : book_id
    };
    $.getJSON('managecontroller/modify', term, function(data){
        if(data){
            getall();
            alert('成功修改');
        }
    });
};